<?php

namespace app\api\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;
use think\response\Json;

class HomeService extends ApiBase
{

    public function index(): Json
    {
        $page = input("page", 1, "intval");
        $limit = input("limit", 10, "intval");
        $category_id = input("category_id", 0, "intval");
        $title = $this->request->param("title", "", "trim");

        $serviceBis = new \app\common\business\HomeService();

        $map = [];
        $map[] = ['category_id','=',$category_id];
        !empty($title) && $map[] = ['title','like','%'.$title.'%'];

        try {
            $list = $serviceBis->getListByWhereWithOrder($map,$page,$limit,'is_hot asc,sort desc,id desc','id,title,address,face_url,create_time');
            $count = $serviceBis->getCountByWhere($map);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        $result = [
            'count'=>$count,
            'list'=>$list
        ];
        return Show::success($result);
    }

    public function detail(): Json
    {
        $id = input("id", 0, "intval");

        $serviceBis = new \app\common\business\HomeService();

        try {
            $info = $serviceBis->getNormalById($id);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        !empty($info) && $info = $serviceBis->resetInfo([$info]);
        return Show::success($info);
    }
}
